package org.grc.app.z_project.z_framework.mvc;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet 助手类
 * 
 * @author grc
 * @since 1.0-SNAPSHOT
 */
public final class ServletHelper {

	private static final Logger LOGGER = LoggerFactory.getLogger(ServletHelper.class);
	
	/**
	 * 使每个线程独自拥有一份 ServletHelper实例
	 */
	private static final ThreadLocal<ServletHelper> SERVLET_HELPER_HOLDER = new ThreadLocal<ServletHelper>();
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private ServletHelper (HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}
	
	/**
	 * 初始化
	 */
	public static void init (HttpServletRequest request, HttpServletResponse response) {
		SERVLET_HELPER_HOLDER.set(new ServletHelper(request, response));
	}
	
	/**
	 * 销毁
	 */
	public static void destroy () {
		SERVLET_HELPER_HOLDER.remove();
	}
	
	/**
	 * 获取 Reques对象
	 */
	private static HttpServletRequest getRequest () {
		return SERVLET_HELPER_HOLDER.get().getRequest();
	}
	
	/**
	 * 获取 Response对象
	 */
	private static HttpServletResponse getResponse () {
		return SERVLET_HELPER_HOLDER.get().getResponse();
	}
	
	/**
	 * 获取 Session对象
	 */
	private static HttpSession getSession () {
		return getRequest().getSession();
	}
	
	/**
	 * 获取 ServletContext对象
	 */
	private static ServletContext getServletContext  () {
		return getRequest().getServletContext();
	}
	
	/**
	 * 将属性放入 Request中
	 */
	public static void setRequestAttribute (String key, Object value) {
		getRequest().setAttribute(key, value);
	}
	
	/**
	 * 从 Request中获取属性
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getRequestAttribute (String key) {
		return (T) getRequest().getAttribute(key);
	}
	
	/**
	 * 从 Request中移除属性
	 */
	public static void removeRequestAttribute (String key) {
		getRequest().removeAttribute(key);
	}
	
	/**
	 * 发送重定向响应
	 */
	public static void sendRedirect (String location) {
		try {
			getResponse().sendRedirect(getRequest().getContextPath() + location);
		} catch (IOException e) {
			LOGGER.error("redirect failure", e);
		}
	}
	
	/**
	 * 将属性放入 Session中
	 */
	public static void setSessionAttribute (String key, Object value) {
		getSession().setAttribute(key, value);
	}
	
	/**
	 * 从 Session中获取属性
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getSessionAttribute (String key) {
		return (T) getSession().getAttribute(key);
	}
	
	/**
	 * 从 Session中移除属性
	 */
	public static void removeSessionAttributte (String key) {
		getSession().removeAttribute(key);
	}
	
	/**
	 * 使 Session失效
	 */
	public static void invalidateSession () {
		getSession().invalidate();
	}
}
